package de.hs_heilbronn;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class Database {
	// Zugangsdaten zur DB-Verbindung
	private String driver = "com.mysql.jdbc.Driver";
	private String db = "jdbc:mysql://db4free.net/vsbib_hhn";
	private String user = "vsbib_hhn";
	private String pass = "vsbib_pw";
	
	// Methode zum Aufbauen der DB-Verbindung und Speichern des letzten abgerufenen Wertes in der Spalte 'Name'
	public ResultSet connectQuery(String sql) {
		ResultSet rs = null;
		try {
			Class.forName(driver);
			Connection con = DriverManager.getConnection(db, user, pass);
			Statement stmt = con.createStatement(
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_UPDATABLE);
			rs = stmt.executeQuery(sql);
			return rs;

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	public boolean connectUpdate(String sql) {
		boolean sqlFehler = false;
		try {
			Class.forName(driver);
			Connection con = DriverManager.getConnection(db, user, pass);
			Statement stmt = con.createStatement(
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_UPDATABLE);
			stmt.executeUpdate(sql);
//			con.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
			sqlFehler=true;
		}
		return sqlFehler;
	}
	
		public boolean fillConnect(String sql) {	
		return connectUpdate(sql);
	}
		
}
